package com.sand.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class FileUpLoad {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/11/17/017 11:45
 */

    //单文件上传
    public static String upFile(MultipartFile file, HttpServletRequest request){
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replaceAll("-","")+suffixName;
        String name="";

        String path1 = FileUpLoad.class.getResource("/").getPath();
        String outPath = path1.substring(0, path1.indexOf("out"));

        //文件上传目录
//        String path = request.getSession().getServletContext().getRealPath("/")+"myFile/";
        String path = request.getSession().getServletContext().getRealPath("/sand_img/");
        //创建文件存放目录
        File file1 = new File(path);
        File fileCopy=new File(outPath);
        //判断目录是否存在
        if(!file1.exists() || !file1.isDirectory()){
            file1.mkdir();
        }
        if(!fileCopy.exists() || !fileCopy.isDirectory()){
            fileCopy.mkdir();
        }

        //文件存放路径
        String upFile = path+uuid;
        //创建文件
        File file2 = new File(upFile);

        //将文件写入目录
        try {
            file.transferTo(file2);

            FileInputStream fin = new FileInputStream(file2);
            FileOutputStream fout = new FileOutputStream(fileCopy.getPath()+"/web/renting/img//"+uuid);

            byte[] by=new byte[1024];
            int leng=0;
            while ((leng=fin.read(by))>0){
                fout.write(by,0,leng);
            }

            fin.close();
            fout.close();

            name=uuid;
            return name;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    //多文件上传
    public static List<String> upFiles(MultipartFile[] files, HttpServletRequest request){
        ArrayList<String> names = new ArrayList<>();
        if(files!=null && files.length>0){
            for (int i = 0; i < files.length; i++) {
                String name = upFile(files[i], request);
                if(!"".equals(name)){
                    names.add(name);
                }
            }
            return names;
        }
        return null;
    }

    //文件下载
    public static boolean loadFile(String fileName,HttpServletRequest request,HttpServletResponse response){
        //创建要下载的文件
        String path = request.getSession().getServletContext().getRealPath("/")+"myFile/"+fileName;
        File file1 = new File(path);

        try {
            //设置响应头
            response.setHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
            //设置编码
            response.setCharacterEncoding("utf-8");
            //二进制传输
            response.setContentType("multipart/form-data");

            //读取文件
            FileInputStream fin = new FileInputStream(file1);
            //输出流
            ServletOutputStream out = response.getOutputStream();
            //写出文件
            byte[] buff=new byte[1024];
            int leng=0;
            while ((leng=fin.read(buff))>0){
                out.write(buff,0,leng);
                out.flush();
            }
            fin.close();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
